<?php
/* TODO: Add code here */
require('config/globalconfig.php');
?>
<?php
include_once('classes/user_sphinx_search.php');
include_once('classes/user_catalogue.php');
//user_common::writeLog('insert',0);
include_once('classes/user_section.php');
include_once('classes/user_property.php');
include_once('classes/user_faq.php');
include_once('classes/editor.php');
$objSection = new user_section($objConnection);
$objCatalogue = new user_catalogue($objConnection);
$objProperty = new user_property($objConnection);
$objFAQ = new user_faq($objConnection);
if ($_pgR["act"]==user_catalogue::ACT_ADD)
{
	if (user_common::isCLogin())
	{
		//lấy thông tin user
		//$c_userInfo = $_SESSION[consts::SES_C_USERINFO];
		
		
		$strName = $_pgR['name'];
		$strName = editor::rteSafe(html_entity_decode($strName,ENT_COMPAT ,'UTF-8' ));
		
		$strDetail= $_pgR['detail'];
		$strSectionID= $_pgR['sect'];
		$strTree = $_pgR['tree'];
		$strTree = editor::rteSafe(html_entity_decode($strTree,ENT_COMPAT ,'UTF-8' ));
		$strHTMLTree = $_pgR['tree_html'];
		//$strHTMLTree = editor::rteSafe(html_entity_decode($strHTMLTree,ENT_COMPAT ,'UTF-8' ));
		$resultID = $objCatalogue->insert($strName,$strDetail,$strSectionID,$strTree,$strHTMLTree);
		
		$strProperty = urldecode($_pgR['property']);
		$strProperty = editor::rteSafe(html_entity_decode($strProperty,ENT_COMPAT ,'UTF-8' ));
		
		$arrProperty = explode(user_common::STRING_SEPARATE_1,$strProperty);
		$strNameProperty = $arrProperty[0];
		$strValueProperty = $arrProperty[1];
		$objProperty->insertPropertyBatch($resultID,$strSectionID,'2', $strNameProperty,$strValueProperty) ;
		
		//thêm phần bang cau hoi
		$strFAQ = urldecode($_pgR['faq']);
		$strFAQ = editor::rteSafe(html_entity_decode($strFAQ,ENT_COMPAT ,'UTF-8' ));
		
		$arrFAQ = explode(user_common::STRING_SEPARATE_1,$strFAQ);
		//print_r($arrProperty);
		$strQuestion = $arrFAQ[0];
		$strAnswer = $arrFAQ[1];
		
		$objFAQ->insertFaqBatch($resultID,'2', $strQuestion,$strAnswer) ;
		
		if ($resultID)
		{
			
			$objSphinx = new user_sphinx_search($objConnection,'t_property');
			$objSphinx->refeshDataFromBackupTable();
			$objSphinx->buildMainIndex();
			
			$objSphinx = new user_sphinx_search($objConnection,'t_faq');
			$objSphinx->refeshDataFromBackupTable();
			$objSphinx->buildMainIndex();
			$arrHeader = user_common::getMessageHeaderArr($banCode);//$banCode
			//echo $strHeaderMessage."1".consts::SEP_RESPONSE.$strResultHTML.consts::SEP_RETURN.$_pgR["ontop"];
			echo user_common::convertToXML(
					$arrHeader, array("rs", "inf"), 
					array(1, $result ), 
					array( 0, 1 )
					);
			return;
		}
		else
		{
			echo user_common::convertToXML($arrHeader, array("rs"), array(0), array(0));
			return;
		}
	}
	else
	{
		echo user_common::convertToXML($arrHeader, array("rs",'info'), array(0,user_common::STRING_REQUIRE_LOGIN), array(0,1));
	}
	return;
}
elseif($_pgR['act'] == user_catalogue::ACT_UPDATE)
{
	if (user_common::isCLogin())
	{
		//lấy thông tin user
		//$c_userInfo = $_SESSION[consts::SES_C_USERINFO];
		
		$strID = $_pgR['id'];
		$strName = $_pgR['name'];
		$strDetail= $_pgR['detail'];
		$strSectionID= $_pgR['sect'];
		$strTree = $_pgR['tree'];
		$strTree = editor::rteSafe(html_entity_decode($strTree,ENT_COMPAT ,'UTF-8' ));
		$strHTMLTree = $_pgR['tree_html'];
		//$strHTMLTree = editor::rteSafe(html_entity_decode($strHTMLTree,ENT_COMPAT ,'UTF-8' ));
		$resultID = $objCatalogue->update($strID,$strName,$strDetail,$strSectionID,$strTree,$strHTMLTree);
		$objFAQ->deleteFAQByProduct($resultID,2);
		$objProperty->deletePropertyByProduct($resultID,2);
		
		$strProperty = urldecode($_pgR['property']);
		$strProperty = editor::rteSafe(html_entity_decode($strProperty,ENT_COMPAT ,'UTF-8' ));
		
		$arrProperty = explode(user_common::STRING_SEPARATE_1,$strProperty);
		//print_r($arrProperty);
		$strNameProperty = $arrProperty[0];
		$strValueProperty = $arrProperty[1];
		$objProperty->insertPropertyBatch($resultID,$strSectionID,'2', $strNameProperty,$strValueProperty) ;
		
		//thêm phần bang cau hoi
		$strFAQ = urldecode($_pgR['faq']);
		$strFAQ = editor::rteSafe(html_entity_decode($strFAQ,ENT_COMPAT ,'UTF-8' ));
		
		$arrFAQ = explode(user_common::STRING_SEPARATE_1,$strFAQ);
		//print_r($arrProperty);
		$strQuestion = $arrFAQ[0];
		$strAnswer = $arrFAQ[1];
		
		$objFAQ->insertFaqBatch($resultID,'2', $strQuestion,$strAnswer) ;
		
		if ($resultID)
		{
			$objSphinx = new user_sphinx_search($objConnection,'t_property');
			$objSphinx->refeshDataFromBackupTable();
			$objSphinx->buildMainIndex();
			
			$objSphinx = new user_sphinx_search($objConnection,'t_faq');
			$objSphinx->refeshDataFromBackupTable();
			$objSphinx->buildMainIndex();
			
			$arrHeader = user_common::getMessageHeaderArr($banCode);//$banCode
			//echo $strHeaderMessage."1".consts::SEP_RESPONSE.$strResultHTML.consts::SEP_RETURN.$_pgR["ontop"];
			echo user_common::convertToXML(
					$arrHeader, array("rs", "inf"), 
					array(1, $result ), 
					array( 0, 1 )
					);
			return;
		}
		else
		{
			echo user_common::convertToXML($arrHeader, array("rs"), array(0), array(0));
			return;
		}
	}
	else
	{
		echo user_common::convertToXML($arrHeader, array("rs",'info'), array(0,user_common::STRING_REQUIRE_LOGIN), array(0,1));
	}
	return;
}
elseif($_pgR['act'] == user_catalogue::ACT_CHANGE_PAGE)
{
	$intPage = $_pgR['p'];
	
	$outPutHTML =  $objCatalogue->getListcatalogue($intPage);
	echo user_common::convertToXML($strMessageHeader, array('rs','inf'), array(1,$outPutHTML),array(0,1));
	return ;
}
elseif($_pgR['act'] == user_catalogue::ACT_SHOW_EDIT)
{
	
	$catalogue_id = $_pgR['id'];
	$arrcatalogue =  $objCatalogue->getCatByID($catalogue_id);
	//echo $arrcatalogue;
	$arrFAQ = $objFAQ->getFaqByProduct($catalogue_id,2);
	$strFAQ ="";
	foreach($arrFAQ as $item)
	{
		$strFAQ.= $item['question'].user_common::STRING_SEPARATE.$item['answer'].user_common::STRING_SEPARATE_1;
	}
	$arrProperty = $objProperty->getPropertyByProduct($catalogue_id,2);
	$strProperty ="";
	foreach($arrProperty as $item)
	{
		$strProperty.= $item['name'].user_common::STRING_SEPARATE.$item['value'].user_common::STRING_SEPARATE_1;
	}
	
	
	echo user_common::convertToXML($strMessageHeader, array('rs','id','name','detail','sect','faq','property'), array(1,$arrcatalogue['cat_id'],$arrcatalogue['name'],$arrcatalogue['detail'],$arrcatalogue['section_id'],$strFAQ,$strProperty),array(0,1,1,1,1,1,1));
	return ;
}
elseif ($_pgR["act"]==user_catalogue::ACT_GET)//user_common::ACT_ADD_LIKE)//add new record like.php Trường hợp content chưa có like_id
{		
	$sectionID = $_pgR["sect"];
	$arrSection= $objCatalogue->getAllCatBySection($sectionID);
	if($arrSection)
	{
		$strHTML = $objCatalogue->outputHTMLCatalogue($arrSection);
		echo user_common::convertToXML($arrHeader, array("rs", "inf"), 
				array(1, $strHTML), array(0, 1));
		return;	
	}
	else
	{
		echo user_common::convertToXML($arrHeader, array("rs",'inf'),array(0,'Không có nhóm hàng'),array(0,0));
		return ;
	}
}

$arrSection = $objSection->getAllSection();
$strHTMLSection = $objSection->outputHTMLSection($arrSection,0);
?>



<?php
include_once('include/_header.inc');
include_once('include/_menu.inc');
?>
<script type="text/javascript" src="<?php echo $_objSystem->locateJs('user_admin_product.js');?>"></script>
<script type="text/javascript" src="<?php echo $_objSystem->locateJs('user_section.js');?>"></script>
<script type="text/javascript" src="<?php echo $_objSystem->locateJs('user_catalogue.js');?>"></script>
<script type="text/javascript" src="<?php echo $_objSystem->locateJs('ajaxupload.js');?>"></script>
<script type="text/javascript" src="<?php echo $_objSystem->locateJs('editor.js');?>"></script>

	<script type="text/javascript" src="ckeditor/ckeditor.js"></script>
<style>
	.ui-button { margin-left: -1px; }
	.ui-button-icon-only .ui-button-text { padding: 0.35em; } 
	.ui-autocomplete-input { margin: 0; padding: 0.48em 0 0.47em 0.45em; }
	</style>
<script>
	
	$(function() {
		$( "#combobox" ).combobox();
		$( "#toggle" ).click(function() {
			$( "#combobox" ).toggle();
		});
	});
	$(function() {
		$( "#combobox2" ).combobox();
		$( "#toggle" ).click(function() {
			$( "#combobox2" ).toggle();
		});
	});
	
	</script>
<!--Begin Form Input -->
<input type="hidden" id="adddocmode" name="adddocmode" value="1<?php //echo $intMode;?>" />
<input type="hidden" id="txtPage" name="txtPage" value="<?php echo $_pgR["p"]?$intPage:1;?>" />
<input type="hidden" id="txtID" name="txtID" value="" />
 <center>
<br><h2 align="center">Quản lý nhóm hàng</h2>
		<div class="input-field-border input-field-content" >
				<div id="lgTitle" class="div_admin_group_title" style="">
				<span style="cursor:default; font-family:inherit" id='status-add' name='status-add'>Add Mode</span></div>
				
				<div class="div_admin_group_content_inside" style="width: 100%; top: -20px;">
				  <table id="tblPopUp" style="width: 100%;" border="0" cellpadding="2" cellspacing="0">
				  <tbody>
									  
				  <tr>
					<td width="110"><span style="cursor:default; font-family:inherit">Tên nhóm hàng </span></td>
					<td width="567"><input id="txtName" name="txtName" value="" style="width: 49.5%;"  maxlength="255" type="text"></td>
				  </tr>
				 <tr>
					<td width="110"><span style="cursor:default; font-family:inherit">Ngành hàng </span></td>
					<td width="567">
					<?php echo $strHTMLSection ?>
					</td>
				  </tr>
				 <tr>
				 <tr>
					<td width="110"><span style="cursor:default; font-family:inherit">Chi tiết </span></td>
					<td width="567"><input id="txtDetail" name="txtDetail" value="" style="width: 49.5%;"  maxlength="255" type="text"></td>
				  </tr>
				 <tr >
					<td width="110"><label for="tree_property" style="cursor:default; font-family:inherit">Cấu trúc chức năng</label></td>
					<td >
						<span id="tree_html">
							<select id="tree_property" name="tree_property" value="" style="width: 20%;"  >
						</select>
						</span>
						<input type="textbox" id="tree_property_input" class="" onFocus="this.select()"/>
						<input type="button"  class="btn btn-oliver"  class="" onclick="_objCommon.insertOptionTree('tree_property_input','tree_property');" value="Add"/>
						<input type="button"  class="btn btn-oliver"  class="" value="Delete" onclick=" _objCommon.removeOptionSelected('tree_property');"/>
						<input type="button"  class="btn btn-oliver"  class="" value="Clear" onclick=" _objCommon.clearAllOption('tree_property');"/>
						<input type="button"  class="btn btn-oliver"  class="" value="View" onclick="_objCommon.viewTree('tree_property');"/>
                    </td>
				  </tr>
				</tbody></table>
				</div>
	<div id="tabs">
	<ul>
		<li><a href="#tabs-property">Tính Năng</a></li>
		<li><a href="#tabs-faq">Hỏi Đáp</a></li>
		
	</ul>
	<div id="tabs-property">
		<div id="zone-properties">
			<div class="e-property">
				<select id="combobox" class="property-combobox">
					<option value="0">--Chọn hoặc gõ--</option>
					<option>Chụp hình</option>
					<option>Quay phim</option>
					<option>GPRS</option>
				</select><input type="textbox" id="combobox_input" class="property-input"/>
			</div>
			<div class="e-property">
				<select id="combobox2"   class="property-combobox">
					<option value="0">--Chọn hoặc gõ--</option>
					<option value="3g">3Gs</option>
					<option value="ch2">Chụp hình 2</option>
					<option value="qp2">Quay phim 2</option>
					<option value="GP">GPRS</option>
				</select><input type="textbox" id="combobox2_input" class="property-input"/>
			</div>
		</div>
		<div class='control'>
		<input type='button' id='addSelect' value='add' onClick='_objAdminProduct.addElementProperty("zone-properties")' />
	</div>
	</div>
	<div id="tabs-faq">
		<div id='zone-faq'>
			<div class="e-faq">
				<h2>Câu hỏi thứ 1:</h2>
				<textarea class="question-input" style="width:100%"></textarea>
				<textarea class="answer-input mce_advanced" id="cmt1" name="cmt1" class="" ></textarea>
				<script type="text/javascript">
					CKEDITOR.replace( 'cmt1',{
					filebrowserBrowseUrl : '/ckfinder/ckfinder.html',
					filebrowserImageBrowseUrl : '/ckfinder/ckfinder.html?type=Images',
					filebrowserFlashBrowseUrl : '/ckfinder/ckfinder.html?type=Flash',
					filebrowserUploadUrl : '/ckfinder/core/connector/php/connector.php?command=QuickUpload&type=Files',
					filebrowserImageUploadUrl : '/ckfinder/core/connector/php/connector.php?command=QuickUpload&type=Images',
					filebrowserFlashUploadUrl : '/ckfinder/core/connector/php/connector.php?command=QuickUpload&type=Flash'
				}
						 );
					//	CKEDITOR.replace( 'cmt'1,{			filebrowserBrowseUrl : '/ckfinder/ckfinder.html',			filebrowserImageBrowseUrl : '/ckfinder/ckfinder.html?type=Images',			filebrowserFlashBrowseUrl : '/ckfinder/ckfinder.html?type=Flash',			filebrowserUploadUrl : '/ckfinder/core/connector/php/connector.php?command=QuickUpload&type=Files',			filebrowserImageUploadUrl : '/ckfinder/core/connector/php/connector.php?command=QuickUpload&type=Images',			filebrowserFlashUploadUrl : '/ckfinder/core/connector/php/connector.php?command=QuickUpload&type=Flash'});
				</script>
			
			</div>
			
		</div>
		<div class='control'>
		<input type='button' id='addSelect' value='add' onClick='_objAdminProduct.addElementfaq("zone-faq")' />
	</div>
	</div>
	
</div>

				<div class="div_admin_group_content_inside" style="margin: 4px; display: block;" align="center">		
				  <input id="btnOK" value="OK"  style="width: 50px;" onClick="_objCatalogue.btnSave_OnClick()" type="button" class="btn btn-oliver"> &nbsp;&nbsp;&nbsp;
				  <input id="btnClose" value="Cancel" align="center" style="width: 55px;" onClick="_objCatalogue.showAddMode()" type="button" class="btn btn-oliver">  
			  </div>					
		</div>	
	
		</center>
<!--End Form Input -->

   <div  id="content-admin" >
                    <div align="center">
	                    <h2>Danh sách</h2>									
					</div>
					<div id="list-content" style="padding:10px">
						<?php echo $objCatalogue->getListCatalogue(1) ?>					
						</div>
	</div>

<?php 
//footer
include_once('include/_footer.inc');
?>